package com.jiang.jixiang.ui;

import android.os.Bundle;
import android.text.TextUtils;

import com.jiang.jixiang.R;
import com.jiang.jixiang.api.ApiUrl;
import com.jiang.jixiang.databinding.ActivityRegistinviteBinding;
import com.jiang.jixiang.network.CommonalityModel;
import com.jiang.jixiang.network.NetWorkListener;
import com.jiang.jixiang.network.okHttpModel;
import com.ws.universal.tools.base.BaseActivity;
import com.ws.universal.tools.base.ManageActivity;
import com.ws.universal.tools.utils.ToastUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 注册成功后填写邀请码
 */
public class RegistInviteActivity extends BaseActivity implements NetWorkListener {
    public String inviter_code;
    private ActivityRegistinviteBinding binding;

    @Override
    protected void initCreate(Bundle savedInstanceState) {
        ManageActivity.putActivity(this.getClass().getSimpleName(), this);
        binding = ActivityRegistinviteBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
    }

    @Override
    protected void initView() {
        binding.inTitle.titleTextTv.setText("");

        binding.btnSkip.setOnClickListener(view -> {
            pageJump(MainActivity.class);
            /*Bundle bundle = new Bundle();
            bundle.putString("hintOne", getString(R.string.registration_success));
            pageJump(SuccessActivity.class, bundle);*/
            finish();
        });

        binding.btnSubmit.setOnClickListener(view -> {
            if (check()) {
                hideSoftInput(binding.btnSubmit);
                inviteCode();
            }
        });

    }

    @Override
    protected void FinishDesTroy() {
        ManageActivity.removeActivity(this.getClass().getSimpleName());
    }

    private boolean check() {
        inviter_code = binding.edInviteCode.getText().toString().trim();
        if (TextUtils.isEmpty(inviter_code)) {
            ToastUtils.showShort(getText(R.string.invite_code_cannot_be_empty));
            return false;
        }
        return true;
    }

    private void inviteCode() {
        showProgressDialog(false);
        Map<String, String> params = new HashMap<>();
        params.put("inviter_code", inviter_code);
        okHttpModel.post(ApiUrl.invitation_Api, params, ApiUrl.invitation_Api_ID, this);
    }

    @Override
    public void onSucceed(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        switch (id) {
            case ApiUrl.invitation_Api_ID:
                pageJump(MainActivity.class);
               /* Bundle bundle = new Bundle();
                bundle.putString("hintOne", getString(R.string.registration_success));
                pageJump(SuccessActivity.class, bundle);*/
                finish();
                ToastUtils.showShort(commonality.getMessage());
                break;
        }

    }

    @Override
    public void onFail(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        ToastUtils.showShort(commonality.getMessage());
    }

    @Override
    public void onError(int id, Exception e) {
        stopProgressDialog();
        ToastUtils.showShort(e.getMessage());
    }
}
